package com.hm.cms.websit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hm.cms.def.TechwebTypeEnum;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.TermsService;
import com.hm.cms.util.WebSitUtil;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年11月19日 上午1:26:44
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
public class TechwebHandler {

	@Autowired
	private TermsService termsService;

	@Autowired
	private ArticleService articleService;

	public void handler() {
		for (TechwebTypeEnum enums : TechwebTypeEnum.values()) {
			System.out.println(enums.getPage());
			for (int i = enums.getPage(); i >= 1; i--) {
				loadMainDirectory(enums.getType() + "/list_" + i + ".shtml#wp", "1",
						enums.getTermTaxonomy());
				WebSitUtil.wating3();
			}
		}
	}

	private void loadArticle(String uri, String operationUserId, String tagId) {
		try {
			if (articleService.checkArticleSourceUri(uri)) {
				return;
			}

			Document document = Jsoup.connect(uri).get();

			String description = document.select("meta[name=description]").get(0).attr("content");
			String keyStr = document.select("meta[name=keywords]").get(0).attr("content");

			Element htmlContent = document.getElementsByClass("article").get(0);

			List<String> termIds = new ArrayList<String>();
			String[] keys = keyStr.split(",");
			for (String key : keys) {
				if (StringUtil.isBlankOrNull(key)) {
					continue;
				}
				HcmsTermsVo hcmsTermsVo = termsService.getHcmsTermsByTermName(key);
				if (CommonUtil.isNotEmpty(hcmsTermsVo)) {
					termIds.add(hcmsTermsVo.getId());
					continue;
				}

				HcmsTermsVo termsVo = new HcmsTermsVo();
				termsVo.setTermName(key);
				termsVo.setTermCode(key);
				termsVo.setTermOrder(-1);
				termIds.add(termsService.addTerms(termsVo, operationUserId));
			}

			String title = StringUtil
					.dislodgeAllBlank(WebUtil.clearHtmlTags(htmlContent.getElementsByClass("title").get(0).html()));
			System.out.println("====>" + title);
			String content = document.getElementById("artibody").html();
			if (StringUtil.isBlankOrNull(description)) {
				description = StringUtil.dislodgeAllBlank(WebUtil.clearHtmlTags(content)).substring(0, 150);
			}
			HcmsArticleVo articleVo = new HcmsArticleVo();
			{
				articleVo.setTermIds(termIds);
				articleVo.setTermTayIds(Arrays.asList(tagId));
				articleVo.setArticleContent(content);
				articleVo.setAuthorName("金三");
				articleVo.setReleaseTime(System.currentTimeMillis());
				articleVo.setTitle(title);
				articleVo.setKeywords(StringUtil.isBlankOrNull(keyStr) ? title : keyStr);
				articleVo.setSourceName("techweb");
				articleVo.setSourceUri(uri);
				articleVo.setArticleOrder((int) articleService.getArtilcleMaxOrder());
				articleVo.setArticleExcerpt(description);
			}
			articleService.addArticle(articleVo, operationUserId);

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	public static void main(String[] args) throws Exception {
		String uri = "http://ai.techweb.com.cn/2017-11-17/2607594.shtml";
		Document document = Jsoup.connect(uri).get();

		String description = document.select("meta[name=description]").get(0).attr("content");
		String keyStr = document.select("meta[name=keywords]").get(0).attr("content");

		Element htmlContent = document.getElementsByClass("article").get(0);
		System.out.println(htmlContent.getElementsByClass("title").get(0).html());

		System.out.println(document.getElementById("artibody"));
	}

	private void loadMainDirectory(String uri, String operationUserId, String tagId) {
		try {
			// String uri = "http://ai.techweb.com.cn/list_1.shtml#wp";
			Document document = Jsoup.connect(uri).get();
			Element element = document.getElementById("wp");

			Elements elements = element.getElementsByTag("h2");
			if (!elements.hasText()) {
				Elements elements2 = document.getElementsByClass("con_one");
				for (Element el : elements2) {
					String path = el.getElementsByTag("h2").get(0).getElementsByTag("a").attr("href");
					loadArticle(path, operationUserId, tagId);
				}
				return;
			}
			for (Element el : elements) {
				System.out.println(el.getElementsByTag("a").attr("href"));
				loadArticle(el.getElementsByTag("a").attr("href"), operationUserId, tagId);
				WebSitUtil.wating3();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}
